package org.mogu.adapter;

import java.util.ArrayList;

import org.mogu.db.DBhelper;
import org.mogu.service.InfoSync;
import org.mogu.staticvalue.Convert;
import org.mogu.tools.Imgtools;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.widget.ImageView;
import android.widget.GridView;

public class ImageGroupAdapter extends BaseAdapter {
	private Context mContext;
	SQLiteDatabase readdb = null;
	String lib="sys";
	ArrayList<String[]> imgarrlist=new ArrayList<String[]>();

	public ImageGroupAdapter(Context c,String groupid) {
		mContext = c;
		imgarrlist.clear();
		readdb = DBhelper.getDBHelper(c).getReadableDatabase();
		synchronized (InfoSync.class) {
		String[] una={Convert.uname,groupid};
		Cursor imglist = readdb.rawQuery(
				"select code,appid from photoLib where username=? and groupid=?", una);
		imglist.moveToFirst();
		String[] imgarr;
		while (!imglist.isAfterLast()) {
			imgarr=new String[2];
			imgarr[0]=imglist.getString(0);
			imgarr[1]=imglist.getString(1);
			imgarrlist.add(imgarr);
			imglist.moveToNext();
		}
		imglist.close();
		readdb.close();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return imgarrlist.size();
	}
	
	public String[] getImgArr(int position){
		return imgarrlist.get(position);
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ImageView imageview;
		if (convertView == null) {
			imageview = new ImageView(mContext);
			imageview.setLayoutParams(new GridView.LayoutParams(85, 85));
			imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageview.setPadding(1, 1, 1, 1);
		} else {
			imageview = (ImageView) convertView;
		}
		String[] stra=imgarrlist.get(position);
//		imageview.setImageResource();
		setViewImage(imageview,stra[0]+stra[1]);
		return imageview;
	}
	
	 public void setViewImage(ImageView v, String value) {  
             Bitmap bitmap = Imgtools.getImage(value);
             v.setImageBitmap(bitmap);  
     }  

}
